
	*DO FILE FOR CREATING ALL MAIN TABLES
	
	
	
	****************************************************

	* TABLE 1 : SUMMARY STATISTICS
	 
	****************************************************
	
	use "$sample", clear
	est clear

	label var sem_avmarks "University exam score"
	label var tipi_extraversion "Extraversion score"
	label var tipi_agree "Agreeableness score"
	label var tipi_consc "Conscientiousness score"
	label var tipi_emotion "Emotional stability score"
	label var tipi_openexp "Openness to experience score"


	label var compete "Competitiveness"
	label var over_conf "Overconfidence"
	label var pr_riskamt "Risk preference"

	label var age "Age"
	label var hindu "Religion"
	label var mod_edu_moth "Mother's Education"
	label var mod_edu_fath "Father's Education"
	label var private_school "Private School"
	label var lowinc "Income"

	local control age hindu  private_school lowinc mod_edu_moth mod_edu_fath 
	local coglist sem_avmarks 
	local traitlist  tipi_extraversion tipi_agree tipi_consc tipi_emotion tipi_openexp 
	local behavlist compete over_conf  pr_riskamt 
	
	*generating Table 1
	eststo pool: quietly estpost summarize `coglist' `behavlist' `traitlist' `control' if abs(runvar)<5 
	eststo male: quietly estpost summarize `coglist' `behavlist' `traitlist' `control' if abs(runvar)<5 & fem==0
	eststo female: quietly estpost summarize `coglist' `behavlist' `traitlist' `control' if abs(runvar)<5 & fem==1
	eststo diff: quietly estpost ttest `coglist' `behavlist' `traitlist' `control' if abs(runvar)<5, by(fem) 
	esttab pool male female diff using "$out/tab_1.tex", ///
	   refcat(sem_avmarks "\textbf{Panel A: Cognitive attainment}" compete "\addlinespace \textbf{Panel B: Economic preferences}" tipi_extraversion "\addlinespace \textbf{Panel C: Personality traits}" age "\addlinespace \textbf{Panel D: Background characteristics}", nolabel) ///
	   cells("mean(pattern(1 1 1 0) fmt(2)) b(pattern(0 0 0 1) star fmt(2))" sd(pattern(1 1 1 0) fmt(2) par)) starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles collabels(none) nonum booktabs  noobs label fragment plain replace


	est clear
	
	
	
	
	
	****************************************************

	* TABLE 2 : FIRST STAGE DISCONTINUITY
	 
	****************************************************
	
	local coglist sem_avmarks  
	local behavlist z_compete z_over_conf z_pr_riskamt 
	local traitlist z_tipi_extraversion z_tipi_agree z_tipi_consc z_tipi_emotion z_tipi_openexp 
	local control age hindu  private_school lowinc mod_edu_moth mod_edu_fath 	
	
	use "$sample", clear
	est clear
	
	*First estimating results in row 1 (without controls)
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  i.cutoffid i.smth if abs(runvar)<5 , cluster(bin)
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  i.cutoffid i.smth if fem==0 & abs(runvar)<5 , cluster(bin)
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  i.cutoffid i.smth if fem==1 & abs(runvar)<5 , cluster(bin)
	
	label var abovecutoff4 "Without controls"
	esttab using "$out/tab_2.tex", b se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(abovecutoff4) label nomtitles nonumbers nolines  fragment nogaps replace 	

	*Next estimating results in row 2 (with controls)	
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  `control' i.cutoffid i.smth if abs(runvar)<5 , cluster(bin)
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  `control' i.cutoffid i.smth if fem==0 & abs(runvar)<5 , cluster(bin)
	eststo: xi: reg tp abovecutoff4 runvar runvar2 runvarint runvar2int  `control' i.cutoffid i.smth if fem==1 & abs(runvar)<5 , cluster(bin)
	label var abovecutoff4 "With controls"
	esttab est4 est5 est6 using "$out/tab_2.tex", b se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(abovecutoff4) label refcat(abovecutoff4 "", nolabel)  nomtitles nonumbers nolines fragment nogaps append 

	
	est clear
	
	
	****************************************************

	* TABLE 3 : AVERAGE PEER QUALITY
	 
	****************************************************
	use "$sample", clear
	est clear

	local control age mod_edu_moth mod_edu_fath hindu private_school lowinc 

		
	*** first generating results for full sample (col1)
	foreach var in peermean_12 peermean_10 {
		xi: ivregress 2sls  `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2 `control' i.cutoffid i.smth if abs(runvar)<5, cluster(bin)
		est store `var'pool
	}
	
	
	*** next generating results by gender (col 2 & 3, by loop)
	foreach var in peermean_12 peermean_10 {
		forvalues i= 0(1)1 {
			xi: ivregress 2sls  `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2 `control' i.cutoffid  i.smth if fem==`i' & abs(runvar)<5, cluster(bin)
			est store `var'`i'
			}
		}

			
	*generating table 3		
	label var tp "Av. grade 12 score"
	esttab peermean_12* using "$out/tab_3.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines obs fragment nogaps replace 
	
	label var tp "Av. grade 10 score"
	esttab peermean_10* using "$out/tab_3.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) refcat(tp "", nolabel) label nomtitles nonumbers nolines obs fragment nogaps append

	
	est clear
	
	
		
	
	****************************************************

	* TABLE 4 : MAIN RESULTS
	 
	****************************************************
	local coglist sem_avmarks  
	local behavlist z_compete z_over_conf z_pr_riskamt 
	local traitlist  z_tipi_extraversion z_tipi_agree z_tipi_consc z_tipi_emotion z_tipi_openexp 
	local control age hindu  private_school lowinc mod_edu_moth mod_edu_fath 
	
	use "$sample", clear
	est clear
	
	putexcel set "$out/mult_pval_results", sheet(pooled, replace) replace //P-values required for multiple hypothesis tests will be stored in this excel file
	putexcel A1="Variable name"
	putexcel B1="Gender p-val"
	local row=2
	
	*** first generating results for full sample (PANEL A)
	foreach var in `coglist' `behavlist' `traitlist' {
		xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2 `control' i.cutoffid i.smth if abs(runvar)<5 , cluster(bin)
		est store fmain_`var'
		
		tempname M
		matrix `M' = r(table)
		local p_val = `M'[4,1]
		local p_val: display %9.3f `p_val'
		local val_lab: variable label `var'

			
		putexcel A`row'=("`val_lab'") 
		putexcel B`row'=(`p_val') 
		
		local row=`row'+1
	}

	
	*** next generating results by gender (PANELS B & C, by loop)
	forvalues i= 0(1)1 {
		putexcel set "$out/mult_pval_results", sheet(gender_`i', replace) modify //Results will be stored in this excel file
		putexcel A1="Variable name"
		putexcel B1="Gender p-val"
		local row=2
		foreach var in `coglist' `behavlist' `traitlist' {
			xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2  `control' i.cutoffid i.smth if fem==`i' & abs(runvar)<5 , cluster(bin)
			est store main_`i'_`var'
			
			tempname M
			matrix `M' = r(table)
			local p_val = `M'[4,1]
			local p_val: display %9.3f `p_val'
			local val_lab: variable label `var'

			
			putexcel A`row'=("`val_lab'") 
			putexcel B`row'=(`p_val') 
		
			local row=`row'+1
		
			}
		}

	**Generating table 4
	label var tp "Enrolled in a selective college"
	esttab fmain* using "$out/tab_4.tex", b(3) se(3) ///
		refcat(tp "\textbf{Panel A: Full Sample}", nolabel) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps replace 	
	esttab main_0* using "$out/tab_4.tex", b(3) se(3) ///
		refcat(tp "[1em] \textbf{Panel B: Males}", nolabel) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps append	
	esttab main_1* using "$out/tab_4.tex", b(3) se(3) ///
		refcat(tp "[1em] \textbf{Panel C: Females}", nolabel) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps append  
	
	
	**Now conduct the Multiple Hypothesis tests 
	***STEP 1: open the "$out/mult_pval_results.xlsx" Excel file with the stored p-values. This file has 3 sheets - pooled, gender_0(Males), gender_1(Females)
	***STEP 2: For each sheet, and each family of outcomes - Cognitive attainment, Behavioral preferences, Personality Traits - conduct the ///
	    //    Anderson multiple hypothesis tests using the do file   "_DMSS_for multiple hypothesis tests - michael anderson code for sharpened FDR adjusted q-values.do"
	***STEP 3: Enter the results into the table by hand
	
	
	**** Now - test beta(males)=beta(females)
	***NOTE: Results will be stores in an Excel sheet "gender_pval_results.xlsx". Input these by hand into the final results table
	
	use "$sample", clear

	keep if abs(runvar)<5 //keep required sample
	
	putexcel set "$out/gender_pval_results", replace //Results will be stored in this excel file
	putexcel A1="Variable name"
	putexcel B1="Gender p-val"
	local row=2
	
	foreach var in `coglist' `behavlist' `traitlist'  { 
		//re-run male and female regression as a cross-check
		ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2   `control'  i.cutoffid i.smth if fem==0 , cluster(bin)
		est sto male
		ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2   `control'  i.cutoffid i.smth if fem==1 , cluster(bin)	
		est sto female
	
		preserve
		expand 2
		tab smth, gen(dsmth)
		tab cutoffid, gen(dcutoffid)
		bys data subjectid: g n=_n
		keep if (n==1&fem==0)|(n==2&fem==1)
		foreach j of varlist `control' dsmth* dcutoffid* runvar runvar2 tp runvarint_tp runvar2int_tp abovecutoff4 runvarint runvar2int {
			g `j'_1=`j'*!(n==1&fem==0)
			g `j'_2=`j'*!(n==2&fem==1)
			}
	
		local dumvar dsmth1_1 -	dsmth3_2 dcutoffid1_1 -	dcutoffid26_2
		//Run fully-intereacted model to get the gender differences in treatment effect
		ivregress 2sls `var' (tp_? runvarint_tp_? runvar2int_tp_? = abovecutoff4_? runvarint_? runvar2int_? ) runvar_? runvar2_?  age_? mod_edu_moth_? mod_edu_fath_? ///
			hindu_? private_school_? lowinc_? `dumvar' fem , cluster(bin)	
		est sto stacked
	
		restore
		esttab male female stacked, nogaps mti star(* 0.10 ** 0.05 *** 0.01)
		
		testnl _b[tp_1]=_b[tp_2]	
		local val_lab: variable label `var'
		local p_val=r(p)
		local p_val: display %9.3f `p_val'
			
		putexcel A`row'=("`val_lab'") 
		putexcel B`row'=(`p_val') 
		
		local row=`row'+1
	
		est clear
		}

		

		
	***************************************************

	* TABLE 5 : PATHWAYS
	 
	****************************************************
	
	use "$sample", clear
	est clear
	
	
	local outlist attend75  less_class take_ba_coaching cl_cancel rti_classST_ratio 
	
	*** generating results by gender (panel A and B, by loop)
	forvalues i= 0(1)1 {
		foreach var in `outlist'   {
			xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int) runvar runvar2 `control'  i.cutoffid i.smth if fem==`i' & abs(runvar)<5, cluster(bin)
			est store path`i'_`var'
			}
		}
	
	* creating table 5 (Panel A)
	label var tp "Enrolled in a selective college"
	esttab path0* using "$out/tab5.tex", b(3) se(3) ///
		refcat(tp "\textbf{Panel A: Males}", nolabel) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label  nomtitles nonumbers nolines fragment nogaps replace	
	* creating table 5 (Panel B)	
	esttab path1* using "$out/tab5.tex", b(3) se(3) ///
		refcat(tp "[1em] \textbf{Panel B: Females}", nolabel) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label  nomtitles nonumbers nolines fragment nogaps append	
		
	
	
	****************************************************

	* TABLE 6 : Heterogeneous Returns to College Quality: By College Ranks
	 
	****************************************************
	
	use "$sample", clear
	est clear
	
	local control age mod_edu_moth mod_edu_fath  hindu private_school lowinc 

	
	*First estimating results in Panels A & B
	
	keep if data=="hh" // keep data for rank 1 cutoff only
	
	
	*** next generating results for panel A (Males)
	foreach var in sem_avmarks z_compete z_pr_riskamt z_tipi_extraversion z_tipi_agree z_tipi_consc z_tipi_openexp {
		qui xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int ) runvar runvar2 `control'  i.cutoffid i.smth if  fem==0 & abs(runvar)<5, cluster(bin)
		est store hh0_`var'
		}
	*due to multicollinearity we run a linear RD specification for the following 2 outcomes
	foreach var in z_over_conf  z_tipi_emotion {
		qui xi: ivregress 2sls `var' (tp runvarint_tp = abovecutoff4 runvarint ) runvar  `control'  i.cutoffid i.smth if  fem==0 & abs(runvar)<5, cluster(bin)
		est store hh0_`var'
		}
	
	*generate table 6, Panel A (MALES)
	label var tp "Enrolled in a selective college"
	esttab hh0_sem_avmarks hh0_z_compete hh0_z_over_conf hh0_z_pr_riskamt hh0_z_tipi_extraversion hh0_z_tipi_agree hh0_z_tipi_consc hh0_z_tipi_emotion hh0_z_tipi_openexp using "$out/tab6_panelA.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps replace 
		
	est clear
	

	*** next generating results for females (Panel B)
	foreach var in `coglist' `behavlist' `traitlist'{
		qui xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int ) runvar runvar2 `control'  i.cutoffid i.smth if  fem==1 & abs(runvar)<5, cluster(bin)
		est store hh1_`var'
		}
		
	*generate table 6, Panel B	(FEMALES)
	esttab hh1* using "$out/tab6_panelB.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps replace  

	est clear
	
	
	
	*-- Now estimating results in Panels C & D
	use "$sample", clear
	est clear
	drop if data=="hh" // drop rank 1 cutoff data
	
	
	*** next generating results by gender (by loop)
	forvalues i= 0(1)1 {
	foreach var in `coglist' `behavlist' `traitlist'{
		qui xi: ivregress 2sls `var' (tp runvarint_tp runvar2int_tp = abovecutoff4 runvarint runvar2int ) runvar runvar2 `control'  i.cutoffid i.smth if  fem==`i' & abs(runvar)<5, cluster(bin)
		est store exhh`i'_`var'
		}
	}
	
	*generate table 6, Panel C	(MALES)	
	label var tp "Enrolled in a selective college"
	esttab exhh0* using "$out/tab6_panelC.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps replace 
	*generate table 6, Panel D (FEMALES)	
	esttab exhh1* using "$out/tab6_panelD.tex", b(3) se(3) ///
		star(* 0.10 ** 0.05 *** 0.01) keep(tp) label nomtitles nonumbers nolines fragment nogaps replace  

	
	est clear
	
	
